/*

 * main.c
 *
 *  Created on: 25/09/2013
 *      Author: vitor
 */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "tela.h"
#include "vm.h"

vmInfo vm;

void* threadRotinaTela(void* unused) {
	inicializaTela();
	rotinaTela(&vm);
	finalizaTela();
	pthread_exit(NULL);
}

int main(int argc, char* argv[]) {
	instrInfo currInstr;
	executableFile program;
	pthread_t threadTela;
	int instrucoesExecutadas = 0;

	if (argc < 2) {
		fprintf(stderr,
				"Amigo, por favor informe um programa... assim fica dificil.\n");
		exit(1);
	}

	program = parseProgram(argv[1]);

	initializeVM(&vm, program);

	pthread_create(&threadTela, NULL, &threadRotinaTela, NULL);

	while (vm.running) {
		fetchAndDecode(&vm, &currInstr);
		execute(&vm, currInstr);
		instrucoesExecutadas++;
	}

	pthread_exit(NULL);
}

